package com.gene.service.impl;


import com.gene.mapper.UpmsRoleMapper;
import com.gene.mapper.UpmsUserMapper;
import com.gene.mapper.UpmsUserRoleMapper;
import com.gene.pojo.*;
import com.gene.request.userRegisterRequest;
import com.gene.service.IUpmsUserService;
import com.gene.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class UpmsUserServiceImpl implements IUpmsUserService {

    @Autowired
    private UpmsUserMapper upmsUserMapper;
    @Autowired
    private UpmsUserRoleMapper upmsUserRoleMapper;
    @Autowired
    private UpmsRoleMapper upmsRoleMapper;

    @Override
    public UpmsUser selectUserNameAndPassword(String userName, String password) {
        UpmsUserExample example=new UpmsUserExample();
        UpmsUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(userName);
        List<UpmsUser> upmsUsers = upmsUserMapper.selectByExample(example);
        if(null==upmsUsers||upmsUsers.size()==0){
            return null;
        }
        return upmsUsers.get(0);
    }

    @Override
    public List<UpmsRole> selectUserRole(Integer userId) {
        UpmsRoleExample example=new UpmsRoleExample();
        UpmsUserRoleExample roleExample=new UpmsUserRoleExample();
        UpmsUserRoleExample.Criteria roleExampleCriteria = roleExample.createCriteria();
        roleExampleCriteria.andUserIdEqualTo(userId);
        List<UpmsUserRole> upmsUserRoles = upmsUserRoleMapper.selectByExample(roleExample);
        if(null==upmsUserRoles||upmsUserRoles.size()==0){
            return null;
        }
        UpmsRoleExample.Criteria criteria = example.createCriteria();
        List<Integer>list=new ArrayList<>();
        for (UpmsUserRole role :upmsUserRoles){
            list.add(role.getRoleId());
        }
        criteria.andRoleIdIn(list);
        List<UpmsRole> upmsRoles = upmsRoleMapper.selectByExample(example);
        if(null==upmsRoles||upmsRoles.size()==0){
            return null;
        }
        return upmsRoles;
    }

    @Override
    public int insertUser(userRegisterRequest userRegister) {
        UpmsUser upmsUser=new UpmsUser();
        upmsUser.setCtime(new Date());
        upmsUser.setUsername(userRegister.getUserName());//用户名
        upmsUser.setRealname(userRegister.getRealname());//姓名
        upmsUser.setIdcar(userRegister.getIdcar());//身份证
        System.out.println(MD5Util.generate(userRegister.getPassWord()));
        String generate = MD5Util.generate(userRegister.getPassWord());
        upmsUser.setPassword(generate);//密码
        upmsUser.setPhone(userRegister.getPhone());//手机号码
        return upmsUserMapper.insert(upmsUser);
    }

    @Override
    public UpmsUser selectUserPhone(String idcar) {


        UpmsUserExample example=new UpmsUserExample();
        UpmsUserExample.Criteria criteria = example.createCriteria();
        criteria.andPhoneEqualTo(idcar);
        List<UpmsUser> upmsUsers = upmsUserMapper.selectByExample(example);
        if(null==upmsUsers||upmsUsers.size()==0){
            return null;
        }
        return upmsUsers.get(0);
    }

    @Override
    public UpmsUser selectuserID(Integer userId) {
        return upmsUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updetePassword(UpmsUser user) {
        return upmsUserMapper.updateByPrimaryKey(user);
    }


    /**
     * 我的资料页面  对于用户资料的补充（只有修改）接口
     * @param upmsUser
     * @return
     */
    @Override
    public boolean amend(UpmsUser upmsUser) {
        return upmsUserMapper.amend(upmsUser);
    }
}
